<!DOCTYPE html>
<html lang="en">

<!-- Head tag -->
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="null">
    <meta name="keyword" content="null">
    <meta name="theme-color" content="#600090">
    <meta name="msapplication-navbutton-color" content="#600090">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="#600090">
    <link rel="shortcut icon" href="/assets/favicon.ico">
    <link rel="alternate" type="application/atom+xml" title="张小阳" href="/atom.xml">
    <link rel="stylesheet" href="https://cdn.bootcss.com/animate.css/3.5.2/animate.css">
    <link rel="stylesheet" href="https://cdn.bootcss.com/font-awesome/4.6.3/css/font-awesome.css">
    <title>
        
        canvas基础｜MillZhang&#39;s blog
        
    </title>

    <link rel="canonical" href="http://millzhang.github.io/2017/07/06/2017-note/25.canvas基础/">

    <!-- Bootstrap Core CSS -->
    <link rel="stylesheet" href="/css/bootstrap.min.css">

    <!-- Custom CSS -->
    <link rel="stylesheet" href="/css/blog-style.css">

    <!-- Pygments Github CSS -->
    <link rel="stylesheet" href="/css/syntax.css">
</head>

<!-- hack iOS CSS :active style -->
<body ontouchstart="" class="animated fadeIn">
<!-- Navigation -->
<nav class="navbar navbar-default navbar-custom navbar-fixed-top " id="nav-top" data-ispost = "true" data-istags="false
" data-ishome = "false" >
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header page-scroll">
            <button type="button" class="navbar-toggle">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand animated pulse" href="/">
                <span class="brand-logo">
                    MillZhang
                </span>
            </a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <!-- Known Issue, found by Hux:
            <nav>'s height woule be hold on by its content.
            so, when navbar scale out, the <nav> will cover tags.
            also mask any touch event of tags, unfortunately.
        -->
        <!-- /.navbar-collapse -->
        <div id="huxblog_navbar">
            <div class="navbar-collapse">
                <ul class="nav navbar-nav navbar-right">
                    <li>
                        <a href="/"><i class="fa fa-home"></i>主页</a>
                    </li>
                   <li>
                        <a href="/tags"><i class="fa fa-tags"></i>归档</a>
                    </li>
                    <li>
                        <a href="/about"><i class="fa fa-user"></i>关于</a>
                    </li>
                </ul>
            </div>
        </div>
    </div>
    <!-- /.container -->
</nav>
<script>
    // Drop Bootstarp low-performance Navbar
    // Use customize navbar with high-quality material design animation
    // in high-perf jank-free CSS3 implementation
//    var $body   = document.body;
    var $toggle = document.querySelector('.navbar-toggle');
    var $navbar = document.querySelector('#huxblog_navbar');
    var $collapse = document.querySelector('.navbar-collapse');

    $toggle.addEventListener('click', handleMagic)
    function handleMagic(e){
        if ($navbar.className.indexOf('in') > 0) {
        // CLOSE
            $navbar.className = " ";
            // wait until animation end.
            setTimeout(function(){
                // prevent frequently toggle
                if($navbar.className.indexOf('in') < 0) {
                    $collapse.style.height = "0px"
                }
            },400)
        }else{
        // OPEN
            $collapse.style.height = "auto"
            $navbar.className += " in";
        }
    }
</script>

<!-- Main Content -->

<!--only post-->

<header class="intro-header">
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1 text-center">
                <div class="post-heading">
                    <h1>canvas基础</h1>
                    
                    <span class="meta">
                         作者 MillZhang
                        <span>
                          日期 2017-07-06
                         </span>
                    </span>
                    <div class="tags text-center">
                        
                        <a class="tag" href="/tags/#javascript"
                           title="javascript">javascript</a>
                        
                        <a class="tag" href="/tags/#canvas"
                           title="canvas">canvas</a>
                        
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="post-title-haojen">
        <span>
            canvas基础
        </span>
    </div>
    <div class="post-header-image">
        
            <img src="http://oritfw5nq.bkt.clouddn.com/wall-303.jpg?imageView2/0/format/jpg/interlace/1/q/85|watermark/2/text/QnkgTWlsbHpoYW5n/font/5b6u6L2v6ZuF6buR/fontsize/480/fill/I0ZERkRGRA==/dissolve/56/gravity/SouthEast/dx/10/dy/10|imageslim">
         
    </div>
</header>

<!-- Post Content -->
<article>
    <div class="container">
        <div class="row">
            <!-- Post Container -->
            <div class="col-lg-8 col-lg-offset-1 col-sm-9 post-container">
                <h3 id="Hello-Canvas"><a href="#Hello-Canvas" class="headerlink" title="Hello Canvas"></a>Hello Canvas</h3><blockquote>
<p><a href="https://jsbin.com/cebereb/edit?js,output" target="_blank" rel="external">https://jsbin.com/cebereb/edit?js,output</a></p>
</blockquote>
<h3 id="canvas元素大小和绘图面积大小"><a href="#canvas元素大小和绘图面积大小" class="headerlink" title="canvas元素大小和绘图面积大小"></a><code>canvas</code>元素大小和绘图面积大小</h3><p><code>canvas</code>元素实际上有两套尺寸.一个是元素本身的大小,可通过<code>css</code>来设置,还有一个是元素绘图表面的大小(drawing surface),该大小是通过设置元素<code>width</code>和<code>height</code>属性来控制的.</p>
<p>如果<code>canvas</code>元素的大小不符合其绘图表面的大小,浏览器会对绘图表面进行缩放,使其符合元素的大小.</p>
<h3 id="canvas元素的API"><a href="#canvas元素的API" class="headerlink" title="canvas元素的API"></a><code>canvas</code>元素的API</h3><p>两个属性和三个方法:</p>
<ul>
<li>width</li>
<li>height</li>
<li>getContext()</li>
<li>toDataURL(type,quality)</li>
<li>toBlob(callback,type,args…)</li>
</ul>
<h3 id="2d绘图环境"><a href="#2d绘图环境" class="headerlink" title="2d绘图环境"></a>2d绘图环境</h3><table>
<thead>
<tr>
<th style="text-align:center">属性</th>
<th style="text-align:center">说明</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">canvas</td>
<td style="text-align:center">指向绘图环境所属的canvas对象,ctx.canvas.width等</td>
</tr>
<tr>
<td style="text-align:center">fillStyle</td>
<td style="text-align:center">指定该绘图环境后续的图像填充所使用的颜色,渐变色或图案</td>
</tr>
<tr>
<td style="text-align:center">font</td>
<td style="text-align:center">指定绘图环境所使用的字型</td>
</tr>
<tr>
<td style="text-align:center">globalAlpha</td>
<td style="text-align:center">全局透明度指定(0-1)</td>
</tr>
<tr>
<td style="text-align:center">globalCompsiteOperation</td>
<td style="text-align:center">指定浏览器将某个物体绘制在其他物体上时所使用的方式</td>
</tr>
<tr>
<td style="text-align:center">lineCap</td>
<td style="text-align:center">如何绘制线段的端点,butt(默认),round,square</td>
</tr>
<tr>
<td style="text-align:center">lineWidth</td>
<td style="text-align:center">绘制线段的屏幕像素宽度,必需是double值,且非负</td>
</tr>
<tr>
<td style="text-align:center">lienJoin</td>
<td style="text-align:center">两条线段相交时如何绘制焦点,bevel,round,miter(默认)</td>
</tr>
<tr>
<td style="text-align:center">miterLimit</td>
<td style="text-align:center">如何绘制miter形式的线段焦点</td>
</tr>
<tr>
<td style="text-align:center">shadowBlur</td>
<td style="text-align:center">如何绘制延伸阴影效果,值越高,阴影效果延伸越远</td>
</tr>
<tr>
<td style="text-align:center">shadowColor</td>
<td style="text-align:center">绘制阴影的颜色</td>
</tr>
<tr>
<td style="text-align:center">shadowOffsetX</td>
<td style="text-align:center">以像素为单位,指定阴影效果的水平偏移量</td>
</tr>
<tr>
<td style="text-align:center">shadowOffsetY</td>
<td style="text-align:center">以像素为单位,指定阴影效果的垂直偏移量</td>
</tr>
<tr>
<td style="text-align:center">strokeStyle</td>
<td style="text-align:center">指定对路径描边所用的绘制风格</td>
</tr>
<tr>
<td style="text-align:center">textAlign</td>
<td style="text-align:center">决定fillText()或strokeText()方法进行绘制时,所画文本的水平对齐方式</td>
</tr>
<tr>
<td style="text-align:center">textBaseline</td>
<td style="text-align:center">决定fillText()或strokeText()方法进行绘制时,所画文本的垂直对齐方式.</td>
</tr>
</tbody>
</table>
<h3 id="3d绘图环境"><a href="#3d绘图环境" class="headerlink" title="3d绘图环境"></a>3d绘图环境</h3><p>即<code>WebGL</code>;</p>
<h3 id="Canvas状态的保存和恢复"><a href="#Canvas状态的保存和恢复" class="headerlink" title="Canvas状态的保存和恢复"></a>Canvas状态的保存和恢复</h3><ul>
<li>save</li>
</ul>
<blockquote>
<p>将当前<code>canvas</code>的状态推送到堆栈顶部.<code>canvas</code>状态包括了当前的坐标变换,剪辑区域,以及绘图环境的所有属性.但不包括当前的路径或唯独.</p>
</blockquote>
<ul>
<li>restore</li>
</ul>
<blockquote>
<p>将<code>canvas</code>状态从堆栈顶部弹出.</p>
</blockquote>

                <hr>
                <ul class="pager">
                    
                    <li class="previous">
                        <a href="/2017/07/06/2017-note/26.canvas绘制/" data-toggle="tooltip" data-placement="top"
                           title="canvas绘制">&larr; 上一页</a>
                    </li>
                    
                    
                    <li class="next">
                        <a href="/2017/07/04/2017-note/24.ES6的编程风格/" data-toggle="tooltip" data-placement="top"
                           title="ES6编程风格摘要">下一页 &rarr;</a>
                    </li>
                    
                </ul>
                <div class="comment">
                    <div id="cloud-tie-wrapper" class="cloud-tie-wrapper"></div>
                </div>
            </div>
            <div class="hidden-xs col-sm-3 toc-col">
                <div class="toc-wrap">
                    <ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#Hello-Canvas"><span class="toc-text">Hello Canvas</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#canvas元素大小和绘图面积大小"><span class="toc-text">canvas元素大小和绘图面积大小</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#canvas元素的API"><span class="toc-text">canvas元素的API</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2d绘图环境"><span class="toc-text">2d绘图环境</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3d绘图环境"><span class="toc-text">3d绘图环境</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Canvas状态的保存和恢复"><span class="toc-text">Canvas状态的保存和恢复</span></a></li></ol>
                </div>
            </div>
        </div>

        <div class="row">
            <!-- Sidebar Container -->

            <div class="
                col-lg-8 col-lg-offset-2
                col-md-10 col-md-offset-1
                sidebar-container">

                <!-- Featured Tags -->
                

                <!-- Friends Blog -->
                
            </div>
        </div>

    </div>
</article>
<!-- 网易云音乐-->

<iframe class="cloud-music" frameborder="no" border="0" marginwidth="0" marginheight="0" width=330 height=86 
    src="//music.163.com/outchain/player?type=2&amp;id=18803411&amp;auto=0&amp;height=66">
</iframe>
<!-- 网易云跟帖-->
<script src="https://img1.cache.netease.com/f2e/tie/yun/sdk/loader.js"></script>
<script>
var cloudTieConfig = {
  url: document.location.href, 
  sourceId: "",
  productKey: "40a8ee22b6084862a8907e6902e525fa",
  target: "cloud-tie-wrapper"
};
var yunManualLoad = true;
Tie.loader("aHR0cHM6Ly9hcGkuZ2VudGllLjE2My5jb20vcGMvbGl2ZXNjcmlwdC5odG1s", true);
</script>

<!-- Footer -->
<!-- Footer -->
<footer>
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1 text-center">
                <p class="copyright text-muted">
                    Copyright &copy; MillZhang 2017
                    <br>
                </p>
            </div>
        </div>
    </div>
</footer>

<!-- jQuery -->
<script src="/js/jquery.min.js"></script>

<!-- Bootstrap Core JavaScript -->
<script src="/js/bootstrap.min.js"></script>

<!-- Custom Theme JavaScript -->
<script src="/js/blog.js"></script>

<!-- async load function -->
<script>
    function async(u, c) {
      var d = document, t = 'script',
          o = d.createElement(t),
          s = d.getElementsByTagName(t)[0];
      o.src = u;
      if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
      s.parentNode.insertBefore(o, s);
    }
</script>

<!-- jquery.tagcloud.js -->
<script>
    // only load tagcloud.js in tag.html
    if($('#tag_cloud').length !== 0){
        async("http://oritfw5nq.bkt.clouddn.com/jquery.tagcloud.js",function(){
            $.fn.tagcloud.defaults = {
                //size: {start: 1, end: 1, unit: 'em'},
                color: {start: '#bbbbee', end: '#0085a1'},
            };
            $('#tag_cloud a').tagcloud();
        })
    }
</script>

<!--fastClick.js -->
<script>
    async("//cdn.bootcss.com/fastclick/1.0.6/fastclick.min.js", function(){
        var $nav = document.querySelector("nav");
        if($nav) FastClick.attach($nav);
    })
</script>



<!--wechat title img-->
<img class="wechat-title-img" src="http://oritfw5nq.bkt.clouddn.com/avatar.jpg?imageslim">
<a id="backtop" class="border-bottom" href="javascript:;">回顶部</a>
<script type="application/javascript">
var docOuter = document.body.scrollTop ? document.body : document.documentElement;
var backToTop = function(rate) {
  var doc = document.body.scrollTop ? document.body : document.documentElement;
  var scrollTop = doc.scrollTop;
  var top = function() {
    scrollTop = scrollTop + (0 - scrollTop) / (rate || 2);
    if (scrollTop < 1) {
      doc.scrollTop = 0;
      return;
    }
    doc.scrollTop = scrollTop;
    // 动画gogogo!
    requestAnimationFrame(top);
  };
  top();
};
var topBtn = document.getElementById('backtop'),
    docTop = docOuter.scrollTop;
if(docTop>=300){
  topBtn.style.opacity = 1;
}

topBtn.onclick = function() {
  backToTop(12)
}
window.onscroll = function(e) {
  var doc = document.body.scrollTop ? document.body : document.documentElement;
  var scrollTop = doc.scrollTop;
  if (scrollTop >= 300) {
    topBtn.style.opacity = 1;
  } else {
    topBtn.style.opacity = 0;
  }
}
</script>
</body>

</html>
